package com.spring.httputils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;

import com.spring.tool.App;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class RestClient {
	public static final int PORT = 9090;
	public static final String BASE_URL = "http://119.145.167.182:9090";

	private static AsyncHttpClient client = new AsyncHttpClient();

	public static String sUserId = "";

	private Activity mContext;

	public RestClient(Activity context){
		mContext = context;
	}

	public  void addHeader() {
		if (!"".equals(sUserId)) {
			client.addHeader("userid", sUserId);
		}
		App.getInstance().showProgressDialog(mContext);
	}

	public  void get(String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		addHeader();
		client.get(getAbsoluteUrl(url), params, responseHandler);
	}

	public  void post(String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		addHeader();

		client.post(getAbsoluteUrl(url), params, responseHandler);
	}

	public  void put(String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		addHeader();
		client.put(getAbsoluteUrl(url), params, responseHandler);
	}

	private  String getAbsoluteUrl(String relativeUrl) {
		if (relativeUrl.startsWith("http://")) {
			return relativeUrl;
		}
		return BASE_URL + relativeUrl;
	}

	public  void excutePost(final String protocolVersion,
			final String requestId, final String manuId, final String OS,
			final String language, final String triggerType,
			final String latitude, final String longitude,
			final String testFlag, final Handler handler, final Context context) {

		// String result = null;
		// BufferedReader reader = null;
		// try {
		// HttpClient client = new DefaultHttpClient();
		// HttpPost request = new HttpPost();
		// request.setURI(new URI(BASE_URL));
		// List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		// postParameters.add(new BasicNameValuePair("protocolVersion",
		// protocolVersion));
		// postParameters.add(new BasicNameValuePair("requestId", requestId));
		// postParameters.add(new BasicNameValuePair("manuId", manuId));
		// postParameters.add(new BasicNameValuePair("OS", OS));
		// postParameters.add(new BasicNameValuePair("language", language));
		// postParameters.add(new BasicNameValuePair("triggerType",
		// triggerType));
		// postParameters.add(new BasicNameValuePair("latitude", latitude));
		// postParameters.add(new BasicNameValuePair("longitude", longitude));
		// postParameters.add(new BasicNameValuePair("testFlag", testFlag));
		// UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
		// postParameters);
		// request.setEntity(formEntity);
		//
		// HttpResponse response = client.execute(request);
		// reader = new BufferedReader(new InputStreamReader(response
		// .getEntity().getContent()));
		//
		// StringBuffer strBuffer = new StringBuffer("");
		// String line = null;
		// while ((line = reader.readLine()) != null) {
		// strBuffer.append(line);
		// }
		// result = strBuffer.toString();
		// RegisterBean regist = JsonParser.fromJsonObject(result,
		// RegisterBean.class);
		// handler.obtainMessage(Constances.Regist.REGIST_SUCCESS, regist)
		// .sendToTarget();
		// } catch (Exception e) {
		// e.printStackTrace();
		// LogUtils.e("", e.getMessage());
		// handler.obtainMessage(Constances.Regist.REGIST_FAILER,
		// e.getMessage())
		// .sendToTarget();
		// } finally {
		// if (reader != null) {
		// try {
		// reader.close();
		// reader = null;
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		// }

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				HttpPost post = new HttpPost(BASE_URL);
				// 创建默认的客户端对象
				HttpClient cliet = new DefaultHttpClient();
				// 用list封装要向服务器端发送的参数
				List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
				params.add(new BasicNameValuePair("requestId", requestId));
				params.add(new BasicNameValuePair("manuId", manuId));
				params.add(new BasicNameValuePair("OS", OS));
				params.add(new BasicNameValuePair("language", language));
				params.add(new BasicNameValuePair("triggerType", triggerType));
				params.add(new BasicNameValuePair("latitude", latitude));
				params.add(new BasicNameValuePair("longitude", longitude));
				params.add(new BasicNameValuePair("testFlag", testFlag));
				try {
					// 用UrlEncodedFormEntity来封装List对象
					UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(params);
					// 设置使用的Entity
					post.setEntity(urlEntity);
					try {
						// 客户端开始向指定的网址发送请求
						HttpResponse response = cliet.execute(post);
						// 获得请求的Entity
						HttpEntity entity = response.getEntity();
						InputStream is = entity.getContent();
						// 下面是读取数据的过程
						BufferedReader br = new BufferedReader(
								new InputStreamReader(is));
						String line = null;
						StringBuffer sb = new StringBuffer();
						while ((line = br.readLine()) != null) {
							sb.append(line);
						}
						System.out.println(sb.toString());
					} catch (ClientProtocolException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}).start();

		// ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		// params.add(new BasicNameValuePair("protocolVersion",
		// protocolVersion));
		// params.add(new BasicNameValuePair("requestId", requestId));
		// params.add(new BasicNameValuePair("manuId", manuId));
		// params.add(new BasicNameValuePair("OS", OS));
		// params.add(new BasicNameValuePair("language", language));
		// params.add(new BasicNameValuePair("triggerType", triggerType));
		// params.add(new BasicNameValuePair("latitude", latitude));
		// params.add(new BasicNameValuePair("longitude", longitude));
		// params.add(new BasicNameValuePair("testFlag", testFlag));
		// // String urlString =
		// HttpConnectionUtils.getUrlFromResources(context,
		// // BASE_URL);
		// new HttpConnectionUtils(handler).post(BASE_URL, params);
	}

}
